package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.loan.PhoneBook;
import com.ucredit.babel.common.model.loan.PhoneBookRecord;

@Deprecated
public class PhoneBookParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  Text val = new Text();
	private  Text key= new Text();
	private  String user_id="";
	
	/**
	 * 通话记录基础数据
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parsePhoneRecordBase(Context context,PhoneBookRecord call,String successful) throws IOException, InterruptedException{
		
    	StringBuilder sb = new StringBuilder(200);
    	Job job = call.getJob();
    	CrawlResult result=job.getResult();
    	if(result==null){
    		result = new CrawlResult();
    	}
    	String s="";
    	//主键,身份证+job_id+时间
    	  user_id=generateId(call.getUserKey()==null?"":call.getUserKey(),job.getJobID(),String.valueOf(call.getTimestamp()));
    	sb.append(user_id).append(CRT001);
    	//user_key
    	sb.append(call.getUserKey()==null?"":call.getUserKey()).append(CRT001);
    	sb.append(call.getTimestamp()).append(CRT001);
    	//job_id
    	sb.append(job.getJobID()==null?"":job.getJobID()).append(CRT001);
    	//是否结束戳
    	sb.append(job.isFinished()==null?"":job.isFinished()).append(CRT001);
    	//是否请求
    	sb.append(job.isRequested()==null?"":job.isRequested()).append(CRT001);
    	//是否成功
    	sb.append(successful).append(CRT001);
    	//起始时间
    	sb.append(job.getStartTime()==null?"":job.getStartTime().getTime()).append(CRT001);
    	//结束时间
    	sb.append(job.getEndTime()==null?"":job.getEndTime().getTime()).append(CRT001);
    	//数据文件
    	sb.append(job.getDataFile()==null?"":job.getDataFile()).append(CRT001);
    	//系统ID
    	sb.append(job.getSystemID()==null?"":job.getSystemID()).append(CRT001);
    	//查询编号
    	sb.append(job.getQuery()==null?"":job.getQuery()).append(CRT001);
    	//尝试次数
    	sb.append(job.getTryTimes()==null?"0":job.getTryTimes()).append(CRT001);
    	//JOB类型
    	sb.append(result.getType()==null?"":result.getType()).append(CRT001);
    	//返回消息
    	sb.append(result.getMessage()==null?"":result.getMessage()).append(CRT001);
    	//代码
    	sb.append(result.getCode()).append(CRT001);
    	//时间戳
    	sb.append(call.getTimestamp());
    	s = sb.toString().replace("\n", " ");
    	key.set("phoneBookJob");
    	val.set(s);
    	context.write(key, val);
	}
	
	private static String generateId(String userkey,String jobid,String ts){
		 
		return MD5Util.getMd5(userkey+jobid+ts);
	}
	private static String generateId(String userkey,String jobid,String ts,String mobile){
		 
		return MD5Util.getMd5(userkey+jobid+ts);
	}
	/**
	 * 通话记录明细
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parsePhoneRecordDetail(Context context,PhoneBookRecord call) throws IOException, InterruptedException{
		
    	StringBuilder sb = new StringBuilder(200);
    	Job job = call.getJob();
    	String s="";
    	List<PhoneBook> list =call.getPhoneBooks();
    	int i=0;
    	for(PhoneBook c :list){
    		i++;
    		
    		sb.append(user_id).append(CRT001);
    		//时间戮
    		sb.append(call.getTimestamp()).append(CRT001);
    		//序号
    		sb.append(i).append(CRT001);
    		//通讯录人名
    		sb.append(c.getName()==null?"":c.getName()).append(CRT001);
    		//通讯录号码
    		sb.append(c.getMobile()==null?"":c.getMobile()).append(CRT001);
    		//公司名称(对应hbase remark)
    		sb.append(c.getRemark()==null?"":c.getRemark()).append(CRT001);
    		//邮件地址
    		sb.append(c.getEmail()==null?"":c.getEmail()).append(CRT001);
    		//职位
    		sb.append(c.getTitle()==null?"":c.getTitle()).append(CRT001);
    		//系统类型
    		sb.append(c.getPhoneSystem()==null?"":c.getPhoneSystem()).append(CRT001);
    		//手机类型
    		sb.append(c.getPhoneType()==null?"":c.getPhoneType()).append(CRT001);
    		//通话次数
    		sb.append(c.getPhoneType()==null?"":c.getPhoneType()).append(CRT001);
    		//最近被联系时间
    		sb.append(c.getCalledTime()==null?"":c.getCalledTime()).append(CRT001);
    		//创建时间
    		sb.append(c.getCreateTime()==null?"":c.getCreateTime()).append(CRT001);
    		//修改时间
    		sb.append(c.getModifyTime()==null?"":c.getModifyTime());
    		s=sb.toString().replace("\n", " ");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("phoneBook");
    		val.set(s);
    		context.write(key, val);
    		sb.delete(0, sb.length());
    	}
    	
	}
}
